package com.qf.ithub.movie_demo01.controller;

import com.qf.ithub.common.result.R;
import com.qf.ithub.common.result.ResponseEnum;
import com.qf.ithub.movie_demo01.common.aop.CheckLogin;
import com.qf.ithub.movie_demo01.common.config.JWTConfig;
import com.qf.ithub.movie_demo01.entity.User;
import com.qf.ithub.movie_demo01.entity.dto.LoginDTO;
import com.qf.ithub.movie_demo01.entity.dto.RoleDTO;
import com.qf.ithub.movie_demo01.entity.dto.SearchDTO;
import com.qf.ithub.movie_demo01.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : xixi
 * @Title :
 * @date :  2021-03-18 08:31
 */
@RestController
@RequestMapping("/api")
@Api(description = "登陆接口")
@CrossOrigin("*")
@Slf4j
public class UserController {

    @Autowired
    private UserServiceImpl userService;


    @Autowired
    private JWTConfig jwtConfig;

    /*获得所有的User*/
    @GetMapping("/user/all")
    public R getNowList(){
        List<User> user = userService.getUser();
        return new R(ResponseEnum.SUCCESS,user);
    }

    /*通过条件查询User*/
    @PostMapping("/user/Search")
    public R getUserList(@RequestBody SearchDTO searchDTO){
        System.out.println(searchDTO);
        List<User> user = userService.getUserBy(searchDTO);
        return new R(ResponseEnum.SUCCESS,user);
    }

    /*登陆*/
    @PostMapping("/user/login")
    public R login(String userName,String userPassword){
        User user = userService.login(userName, userPassword);
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(user,loginDTO);
        LoginDTO dto = LoginDTO.builder()
                .roleid(user.getRoleid())
                .avatarUrl(user.getAvatarUrl())
                .id(user.getId())
                .userName(user.getUserName())
                .build();

        //生成token
        String token = jwtConfig.generateJwt(loginDTO);
        loginDTO.setToken(token);
        return new R(ResponseEnum.SUCCESS,loginDTO);
    }

    @GetMapping("/user/now")
    @CheckLogin
    public R getUserInfo(HttpServletRequest request){
        // 1、获得当前登陆的用户id
        Integer id = new Integer(request.getAttribute("id").toString());
        // 2、通过id获得用户
        User user = userService.getUserByid(id);
        return new R(ResponseEnum.SUCCESS,user);
    }

    /*
    * 验证用户名接口
    * */
    @GetMapping("/user/check/{userName}")
    @CheckLogin
    public R checkName(@PathVariable("userName")String userName, HttpServletRequest request){
        // 1、获得当前登陆的用户id
        Integer id = new Integer(request.getAttribute("id").toString());
        boolean flag = userService.checkUserName(userName, id);

        return new R(ResponseEnum.SUCCESS,flag);
    }

    @PutMapping("/user")
    public R updateUserByID( User user){
        userService.updateUserByID(user);
        return  new R(ResponseEnum.SUCCESS,null);
    }

    @PostMapping("/user/upd")
    public R updateRoleById(@RequestBody RoleDTO roleDTO){
        System.out.println(roleDTO);
        userService.updateroleByID(roleDTO.getId(),roleDTO.getRoles());
        return  new R(ResponseEnum.SUCCESS,null);
    }

}
